package com.xiaomaoguai.webkit.web;

import javax.servlet.ServletContext;

/**
 * @Description :
 * @FileName: XmgWebUtils.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年5月8日 下午2:29:09
 * @Version:V1.00
 */
public class XmgWebUtils {

	public final static String WEB_ROOT_KEY = "webRoot";

	private XmgWebUtils() {
	}

	public static String getInitParameter(ServletContext context, String key) {
		String value = context.getInitParameter(key);
		context.log("Get context param: '" + key + "=[" + value + "]'.");
		return value;
	}
	
	public static void setWebAppRootSystemProperty(String key, ServletContext servletContext) throws IllegalStateException {
		if(key == null) {
			throw new IllegalStateException(
					"Can't set web root system property because the key is null.");
		}
		String root = servletContext.getRealPath("/");
		if (root == null) {
			throw new IllegalStateException(
			    "Cannot set web app root system property when WAR file is not expanded");
		}
		System.setProperty(key, root);
		servletContext.log("Set web app root system property: '" + key + "' = [" + root + "]");
	}
	
	public static String simpleHtmlEntities(String source) {
		return source
				.replaceAll("'", "&#39;")
				.replaceAll("\\\\", "&#92;");
		
	}

}
